package com.osfw.activiti.controller.process;

import com.github.pagehelper.PageInfo;
import com.osfw.activiti.domain.ProcessApplyInfo;
import com.osfw.activiti.domain.ProcessTaskInfo;
import com.osfw.activiti.service.process.IProcessService;
import com.osfw.framework.annotation.Log;
import com.osfw.framework.constants.Constants;
import com.osfw.framework.core.controller.BaseController;
import com.osfw.framework.enums.BusinessType;
import com.osfw.framework.exception.ServiceException;
import com.osfw.system.entity.config.SysConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程运行 Controller
 */
@Controller
@RequestMapping("/osfw/activiti/process")
public class ProcessController extends BaseController {
    private String prefix = "osfw/activiti/process";

    @Autowired
    private IProcessService processService;

    /**
     * 暂存列表
     */
    @RequiresPermissions("activiti:process:view")
    @GetMapping("/temp")
    public String temp() {
        return prefix + "/temp";
    }

    /**
     * 我的申请
     */
    @RequiresPermissions("activiti:process:view")
    @GetMapping("/apply")
    public String apply() {
        return prefix + "/apply";
    }

    /**
     * 我的申请
     */
    @RequiresPermissions("activiti:process:apply")
    @PostMapping("/apply/list")
    @ResponseBody
    public PageInfo applyList(ProcessApplyInfo applyInfo) {
        startPage(ProcessApplyInfo.class);
        List<ProcessApplyInfo> list = processService.selectApplyList(applyInfo, getSysUser());
        return getPageInfo(list);
    }

    /**
     * 撤销申请
     */
    @RequiresPermissions("activiti:process:apply")
    @Log(title = "撤销申请", businessType = BusinessType.DELETE)
    @PostMapping("/apply/cancel")
    @ResponseBody
    public boolean cancel(String ids) {
        return processService.cancelApplyList(ids);
    }

    /**
     * 我的待办
     */
    @RequiresPermissions("activiti:process:view")
    @GetMapping("/todo")
    public String todo() {
        return prefix + "/todo";
    }

    /**
     * 我的申请
     */
    @RequiresPermissions("activiti:process:apply")
    @PostMapping("/todo/list")
    @ResponseBody
    public PageInfo todoList(ProcessTaskInfo taskInfo) {
        startPage(ProcessTaskInfo.class);
        List<ProcessTaskInfo> list = processService.selectTodoTasks(taskInfo, getSysUser());
        return getPageInfo(list);
    }

    /**
     * 我的委托
     */
    @RequiresPermissions("activiti:process:view")
    @GetMapping("/entrust")
    public String entrust() {
        return prefix + "/entrust";
    }


    /**
     * 我的已办
     */
    @RequiresPermissions("activiti:process:view")
    @GetMapping("/done")
    public String done() {
        return prefix + "/done";
    }

    /**
     * 我的申请
     */
    @RequiresPermissions("activiti:process:apply")
    @PostMapping("/done/list")
    @ResponseBody
    public PageInfo doneList(ProcessTaskInfo taskInfo) {
        startPage(ProcessTaskInfo.class);
        List<ProcessTaskInfo> list = processService.selectDoneTasks(taskInfo, getSysUser());
        return getPageInfo(list);
    }

    /**
     * 办结列表
     */
    @RequiresPermissions("activiti:process:completed")
    @GetMapping("/completed")
    public String completed() {
        return prefix + "/completed";
    }

    /**
     * 办结列表
     */
    @RequiresPermissions("activiti:process:completed")
    @PostMapping("/completed/list")
    @ResponseBody
    public PageInfo completedList(ProcessTaskInfo taskInfo) {
        startPage(ProcessTaskInfo.class);
        List<ProcessTaskInfo> list = processService.selectCompletedTasks(taskInfo, getSysUser());
        return getPageInfo(list);
    }

    /**
     * 办理
     */
    @RequiresPermissions("activiti:process:todo")
    @GetMapping("/handle")
    public String handle() {
        return prefix + "/handle";
    }


    /**
     * 新增保存参数配置
     */
    @RequiresPermissions("system:config:todo")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/entrust/{taskId}")
    @ResponseBody
    public boolean entrust(@PathVariable Long taskId) {
        return processService.entrustTodoTask(taskId, getUserId());
    }

    /**
     * 新增保存参数配置
     */
    @RequiresPermissions("system:config:todo")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/sign/{taskId}")
    @ResponseBody
    public boolean sign(@PathVariable Long taskId) {
        return processService.signTodoTask(taskId, getUserId());
    }


    /**
     * 新增保存参数配置
     */
    @RequiresPermissions("system:config:todo")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/refuse/{taskId}")
    @ResponseBody
    public boolean refuse(@PathVariable Long taskId) {
        return processService.refuseTodoTask(taskId);
    }

    /**
     * 新增保存参数配置
     */
    @RequiresPermissions("system:config:todo")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping("/handle")
    @ResponseBody
    public boolean handleSave(@Validated ProcessTaskInfo taskInfo) {
        return processService.handleTodoTask(taskInfo);
    }

    /**
     * 结果
     */
    @RequiresPermissions("activiti:process:view")
    @GetMapping("/result")
    public String result(@PathVariable("configId") Long taskId, ModelMap mmap) {
        mmap.put("taskInfo", processService.getDoneTaskInfo(taskId));
        return prefix + "/taskInfo";
    }

}
